package com.hiraeth.modules.monitor.controller;


import com.hiraeth.common.utils.PageParams;
import com.hiraeth.common.utils.R;
import com.hiraeth.modules.monitor.entity.MonitorDashboard;
import com.hiraeth.modules.monitor.service.MonitorDashboardService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

@RestController
@RequestMapping("/monitor/dashboard")
public class MonitorDashboardController {

    @Autowired
    private MonitorDashboardService monitorDashboardService;

    /**
     * 获取分页仪表盘列表
     *
     * @return
     */
    @GetMapping("/page")
    @RequiresPermissions("monitor:dashboard:page")
    public R getDashboardPage(@RequestParam(required = false) Map map) {
        return R.ok().put("page",monitorDashboardService.findListPage(new PageParams(map)));
    }

    /**
     * 添加仪表盘
     * @return
     */
    @PostMapping("/add")
    @RequiresPermissions("monitor:dashboard:add")
    public R addDashboard(@RequestBody MonitorDashboard monitorDashboard) {
        Long dashboardId = null;
        MonitorDashboard result = monitorDashboardService.addDashboard(monitorDashboard);
        if (result != null) {
            dashboardId = result.getDashboardId();
        }
        return R.ok().put("data",dashboardId);
    }

    /**
     * 更新仪表盘
     *
     * @return
     */
    @PostMapping("/update")
    @RequiresPermissions("monitor:dashboard:update")
    public R updateDashboard(@RequestBody MonitorDashboard monitorDashboard) {
        monitorDashboardService.updateDashboard(monitorDashboard);
        return R.ok();
    }

    /**
     * 删除仪表盘
     *
     * @return
     */
    @PostMapping("/remove")
    @RequiresPermissions("monitor:dashboard:remove")
    public R removeDashboard(@RequestBody Long[] dashboardIds) {
        monitorDashboardService.removeByIds(Arrays.asList(dashboardIds));
        return R.ok();
    }

    /**
     * 根据仪表盘主键查询仪表盘信息
     * @param dashboardId
     * @return
     */
    @GetMapping("/{dashboardId}")
    @RequiresPermissions("monitor:dashboard:info")
    public R getDashboardById(@PathVariable("dashboardId") Long dashboardId){
        return R.ok().put("data", monitorDashboardService.getById(dashboardId));
    }
}
